﻿using System.Windows;
using personalplaner.common.mvvm;
using System.Windows.Input;
using personalplaner.common.mvvm.commands;
using personalplaner.gui.common;
using System.Windows.Controls;
using System.Collections.Generic;
using personalplaner.gui.stamm.views;
using personalplaner.gui.stamm.viewmodels.models;
using personalplaner.business.stamm;
using personalplaner.servicemodel.dataaccess;
using System;
using personalplaner.business;

namespace personalplaner.gui.stamm.viewmodels
{
	public class DienstZeitDialogViewModel : BaseViewModel
	{
		public DienstZeitDialogViewModel(DependencyObject parent)
			: base(parent)
		{
			SelectedView = Views[0];
		}

		#region Implementation

		public override void Loaded()
		{
			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = false;
		}

		private void SaveZeiten()
		{
			var lst = new List<DienstZeit>();

			if (DatumModel.IsZeitVonAbteilung)
			{
				lst.Add(new DienstZeit
				{
					DatumVon = DatumModel.Von,
					DatumBis = DatumModel.Bis,
					IsAbteilungsZeit = DatumModel.IsZeitVonAbteilung,
					Von = "00:00",
					Bis = "00:00",
					ZeitEinheitTyp = business.enums.enZeiteinheit.Arbeit,
					DienstId = DienstId
				});
			}
			else
			{
				foreach (var time in ZeitModel.Zeiten)
				{
					lst.Add(new DienstZeit
					{
						DatumVon = DatumModel.Von,
						DatumBis = DatumModel.Bis,
						IsAbteilungsZeit = DatumModel.IsZeitVonAbteilung,
						Von = time.Von,
						Bis = time.Bis,
						ZeitEinheitTyp = time.ZeitTyp,
						DienstId = DienstId
					});
				}
			}

			var da = GetService<IDiensteDataAccess>();

			// clear the existing parts if it is a update
			if (UpdateCollection != null)
				da.EndDienstZeiten(DienstId, UpdateCollection.DatumVon);

			// clear all other parts
			da.EndDienstZeiten(DienstId, DatumModel.Von.AddDays(-1));

			foreach (var itm in lst)
				da.SetDienstZeit(itm);
		}

		#endregion

		#region Commands

		ICommand _okcommand;
		public ICommand OkCommand
		{
			get
			{
				if (_okcommand == null)
					_okcommand = new RelayCommand(OnOkCommand, parameter => (SelectedView == Views[1] && ZeitModel.Zeiten.Count > 0) || DatumModel.IsZeitVonAbteilung);
				return _okcommand;
			}
		}

		private void OnOkCommand(object parameter)
		{
			SaveZeiten();

			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = true;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		ICommand _cancelcommand;
		public ICommand CancelCommand
		{
			get
			{
				if (_cancelcommand == null)
					_cancelcommand = new RelayCommand(OnCancelCommand);
				return _cancelcommand;
			}
		}

		private void OnCancelCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = false;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		private ICommand _nextcommand;

		public ICommand NextCommand
		{
			get
			{
				if (_nextcommand == null)
					_nextcommand = new RelayCommand(OnNext, parameter => SelectedView != Views[1]);
				return _nextcommand;
			}
		}

		private void OnNext(object parameter)
		{
			for (int i = 0; i < Views.Count; i++)
			{
				if (Views[i] == SelectedView)
				{
					SelectedView = Views[i + 1];
					break;
				}
			}

			RaisePropertyChanged("IsEndOfWizard");
		}

		ICommand _previouscommand;
		public ICommand PreviousCommand
		{
			get
			{
				if (_previouscommand == null)
					_previouscommand = new RelayCommand(OnPrevious, parameter => SelectedView != Views[0]);
				return _previouscommand;
			}
		}

		private void OnPrevious(object parameter)
		{
			for (int i = 0; i < Views.Count; i++)
			{
				if (Views[i] == SelectedView)
				{
					SelectedView = Views[i - 1];
					break;
				}
			}

			RaisePropertyChanged("IsEndOfWizard");
		}

		#endregion

		#region Properties

		DienstZeitWizardZeitModel _zeitModel;
		public DienstZeitWizardZeitModel ZeitModel
		{
			get
			{
				if (_zeitModel == null)
				{
					_zeitModel = new DienstZeitWizardZeitModel
					{
						Von = "07:00",
						Bis = "18:00",
						SelectedZeitTyp = business.enums.enZeiteinheit.Arbeit
					};
				}
				return _zeitModel;
			}
		}

		DienstZeitWizardDatumModel _datumModel;
		public DienstZeitWizardDatumModel DatumModel
		{
			get
			{
				if (_datumModel == null)
				{
					_datumModel = new DienstZeitWizardDatumModel
					{
						Von = DateTime.Now,
						Bis = DateTime.Now.AddYears(20)
					};
					_datumModel.PropertyChanged += datumModelPropertyChanged;
				}
				return _datumModel;
			}
		}

		void datumModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "IsZeitVonAbteilung")
				RaisePropertyChanged("IsEndOfWizard");
		}


		IList<UserControl> _views;
		public IList<UserControl> Views
		{
			get
			{
				if (_views == null)
				{
					_views = new List<UserControl>
					         {
					         	new DienstZeitDatumView {DataContext = DatumModel},
					         	new DienstZeitZeitView {DataContext = ZeitModel}
					         };
				}
				return _views;
			}
		}

		UserControl _selectedView;
		public UserControl SelectedView
		{
			get
			{
				return _selectedView;
			}
			set
			{
				_selectedView = value;
				RaisePropertyChanged("SelectedView");
			}
		}

		public bool IsEndOfWizard
		{
			get
			{
				if (DatumModel.IsZeitVonAbteilung)
					return true;

				if (Views == null || Views.Count == 0)
					return true;

				return SelectedView == Views[Views.Count - 1];
			}
		}

		public int DienstId { get; set; }

		DienstZeitCollection _updateCollection;
		public DienstZeitCollection UpdateCollection
		{
			get
			{
				return _updateCollection;
			}
			set
			{
				_updateCollection = value;
				if (value == null)
					return;

				DatumModel.Von = value.DatumVon;
				DatumModel.Bis = value.DatumBis;
				DatumModel.IsZeitVonAbteilung = value.IsAbteilungsZeit;

				foreach (var itm in value)
				{
					ZeitModel.Zeiten.Add(new DienstZeitEinheit
					{
						Von = itm.Von,
						Bis = itm.Bis,
						ZeitTyp = itm.ZeitEinheitTyp
					});
				}
			}
		}

		#endregion
	}
}
